package com.zuocheng.librarycar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private ArrayList<String> mDatas = new ArrayList<>();
    public RecyclerView mRv;

    private void generateDatas() {
        for (int i = 1; i <= 100; i++) {
            mDatas.add("第 " + i + " 个item");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //transparentStatusBar();
        generateDatas();
        mRv = findViewById(R.id.linearRv);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);//线性布局
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRv.setLayoutManager(linearLayoutManager);

//        RecyclerAdapter adapter = new RecyclerAdapter(getApplicationContext(), mDatas);
//        mRv.setAdapter(adapter);

        setAndroidNativeLightStatusBar(this,true);
        initTitle();

        SharedPreferences sp = getSharedPreferences("yuecun", Context.MODE_PRIVATE);
        String Authorization = sp.getString("Authorization","");
        System.out.println("------"+Authorization);
        if (Authorization.length() > 0){
            sendRequestWithOkHttp();
        }else {
            LoginActivity.start(getApplicationContext());
        }




    }
    @Override
    protected void onRestart() {
        super.onRestart();
        sendRequestWithOkHttp();
    }


    public void initTitle() {

        OverAllTitleBar rlTitle = findViewById(R.id.rl_title);
        rlTitle.tvTitle.setText("我的车辆");

        rlTitle.ivBack.setVisibility(View.GONE);
        rlTitle.ivBack.setImageResource(R.mipmap.ic_mian_nav_left);
        rlTitle.ivBack.setOnClickListener(view -> {
        });

        rlTitle.ivMessage.setVisibility(View.VISIBLE);
        rlTitle.ivMessage.setImageResource(R.mipmap.ic_mian_nav_left);
        rlTitle.ivMessage.setOnClickListener(view -> {
            MineActivity.start(this);
        });
    }

    /**
     * 透明状态栏
     */
    private void transparentStatusBar() {
        //改变状态栏颜色为透明
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
    }
    //设置状态栏字体颜色
    private static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) {
        View decor = activity.getWindow().getDecorView();
        if (dark) {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }



    private void sendRequestWithOkHttp() {
        SharedPreferences sp = getSharedPreferences("yuecun", Context.MODE_PRIVATE);
        String Authorization = sp.getString("Authorization","");
        if (Authorization.length() <= 0){
            return;
        }

        OkHttpUtil.builder().url("https://maishang.yuecunpaimai.com/prod-api/maishangmanager/car/api/list")
                .addHeader("Authorization",Authorization)
                .get().async(new OkHttpUtil.ICallBack() {
            @Override
            public void onSuccessful(Call call, String data) {
                System.out.println("======="+data);

                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(data);
                String code = jsonObject.getString("code");
                if (Long.parseLong(code) == 401){
                    //Toast.makeText(getApplicationContext(),"Token失效",Toast.LENGTH_SHORT).show();

                    LoginActivity.start(getApplicationContext());
                    return;
                }
                //List<com.alibaba.fastjson.JSONObject> data = jsonObject.getJSONObject("data");

                JSONArray dataList = jsonObject.getJSONArray("data");
                List<CarBean> listCar = dataList.toJavaList(CarBean.class);


                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        RecyclerAdapter adapter = new RecyclerAdapter(getApplicationContext(), listCar);
                        mRv.setAdapter(adapter);

                        float pricess = 0;
                        for (int i=0;i<listCar.size();i++){
                            CarBean item = listCar.get(i);
                            pricess = pricess + Float.parseFloat(item.buyPrice);
                        }
                        TextView tv_all_amount = findViewById(R.id.tv_all_amount);
                        tv_all_amount.setText("投入资金：¥"+pricess+"元");
                    }
                });
            }

            @Override
            public void onFailure(Call call, String errorMsg) {

            }
        });
    }

}